// app.js
App({
  call : require("utils/request.js"),
  config : require("siteinfo.js"),
  onLaunch() {
    this.globalData.domain = this.config.siteroot
  },
  showTip:function(title,icon = 'none',dosuccess = function(){}){
    wx.showToast({
        title: title,
        icon: icon,    //如果要纯文本，不要icon，将值设为'none',
        duration: 2000 ,
        success: function () {
           dosuccess()
        }  
      })  
  },
  // getUserInfo(v,that){
  getUserInfo(that){
    var _that = this
     //用户授权登陆，并跳转首页
     wx.login({
        success: function (res) {
            // 请求自己后台获取用户openid
            var postData = {
                // encryptedData: v.encryptedData,
                // signature: v.signature,
                // rawData: v.rawData,
                // iv: v.iv,
                code: res.code
            }
            _that.call.request('user/login',postData, function(data){
                //如果接口返回错误 则提示错误信息
                if(data.code != 0){
                    that.showModal('请授权后使用小程序')
                    return;
                }
                _that.globalData.userInfo = data.data
                //授权成功后，隐藏授权按钮，显示正文
                that.setData({
                    userInfo:data.data,
                });
            },function(){});
        }
    })
  }, 
  getMobile(v,that){
     var _that = this
    wx.login({
      success: function (res) {
          // 请求自己后台获取用户openid
          var postData = {
              encryptedData: v.detail.encryptedData,
              signature: v.detail.signature,
              rawData: v.detail.rawData,
              iv: v.detail.iv,
              code: res.code
          }
          _that.call.request('user_check/getWxappMobile',postData, function(data){
              //如果接口返回错误 则提示错误信息
              if(data.code != 0){
                  that.showModal('请授权后使用小程序')
                  return;
              }
              _that.globalData.userInfo.mobile = data.data
              that.setData({
                'userInfo.mobile':data.data
              });
              
          },function(){});
      }
    })
  },
  //获取用户信息
  user_login(that){
    var _that = this
    wx.login({
      success: function (res) {
        _that.call.getData('user_check/getUserInfo?code='+res.code, function(data){
          //如果接口返回错误 则提示错误信息
          if(data.code != 0){
              return;
          }
          _that.globalData.userInfo = data.data
          that.setData({
            userInfo:data.data,
          });
      },function(){});
      }
    })
  },
  upload(that,path,dosuccess){
    var _that = this
    _that.showTip('加载中...','loading')
    wx.uploadFile({
      url:_that.globalData.domain+'/api/index/upload', //仅为示例，非真实的接口地址
      filePath: path,
      name: 'file',
      success: function (res) {
        var data = JSON.parse(res.data)
        console.log(data)
        if(data.code != 0){
          _that.showTip(data.msg)
          return 
        }
        _that.showTip('上传成功')
        that.setData({
          autograph:data.data
        })
        dosuccess(that)
      }
    })
  },
  uploadFace(path,fun){
    this.showTip('加载中...','loading')
    wx.uploadFile({
      url:this.globalData.domain+'/api/index/uploadFace', //仅为示例，非真实的接口地址
      filePath: path,
      name: 'file',
      success: (res) => {
        res = JSON.parse(res.data)
        console.log(res)
        if(res.code == 0){
          this.showTip('上传成功')
          fun(res.data)
        }else{
          this.showTip(res.msg)
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    domain:null
  }
})
